﻿//======================================================================
//
//        Copyright (C) 2007-2008 三月软件工作室    
//        All rights reserved
//
//        filename :GetTableColumnsByConnection
//        description :
//
//        created by 侯垒 at  2009-11-14 11:18:58
//        http://houleixx.cnblogs.com
//
//======================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;

namespace Entity.Core
{
    public class GetTableColumnsByConnection
    {
        string connection = string.Empty;
        string provider = string.Empty;
        string table = string.Empty;
        DbProviderFactory factory;
        /// <summary>
        /// 构造函数；
        /// </summary>
        /// <param name="connection">数据库链接字符串</param>
        /// <param name="provider"></param>
        /// <returns></returns>
        public GetTableColumnsByConnection(string connection, string provider, string tableName)
        {
            this.connection = connection;
            this.provider = provider;
            this.table = tableName;
            factory = DbProviderFactories.GetFactory(provider);
        }

        /// <summary>
        /// 根据数据库链接得到datareader
        /// </summary>
        /// <returns></returns>
        public DbDataReader GetDataReader()
        {
            DbConnection conn = factory.CreateConnection();
            conn.ConnectionString = connection;
            DbCommand cmd = factory.CreateCommand();
            cmd.Connection = conn;
            cmd.CommandText = string.Format("select * from [{0}]", table);
            conn.Open();
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }
    }
}
